﻿// See https://aka.ms/new-console-template for more information

using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using WorkflowCore.Interface;
using WorkflowCore.Models;

class Program
{
    static void Main(string[] args)
    {
        IServiceProvider serviceProvider = ConfigureServices();
        var host = serviceProvider.GetService<IWorkflowHost>();
        host.RegisterWorkflow<HelloWorldWorkflow>();
        host.RegisterWorkflow<TestFlow, PassData>();
        host.Start();

        host.StartWorkflow("HelloWorld", 1, null);
        Console.ReadLine();
        host.Stop();

        //Console.WriteLine(ExcuteCMD()) ;

        Console.ReadKey();
    }

    private static IServiceProvider ConfigureServices()
    {
        //setup dependency injection
        IServiceCollection services = new ServiceCollection();
        services.AddLogging();
        services.AddWorkflow();

        var serviceProvider = services.BuildServiceProvider();

        return serviceProvider;
    }

    /// <summary>
    /// windows系统命令
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="args"></param>
    /// <returns></returns>
    internal static string Cmd(string fileName, string args)
    {
        string output = string.Empty;

        var info = new ProcessStartInfo();
        info.FileName = fileName;
        info.Arguments = args;
        info.RedirectStandardOutput = true;
        info.CreateNoWindow = false;

        using (var process = Process.Start(info))
        {
            output = process.StandardOutput.ReadToEnd();
        }
        return output;
    }

    private static string ExcuteCMD()
    {
        Process p = new Process();
        //设置要启动的应用程序
        p.StartInfo.FileName = "cmd.exe";
        //是否使用操作系统shell启动
        p.StartInfo.UseShellExecute = false;
        // 接受来自调用程序的输入信息
        p.StartInfo.RedirectStandardInput = true;
        //输出信息
        p.StartInfo.RedirectStandardOutput = true;
        // 输出错误
        p.StartInfo.RedirectStandardError = true;
        //不显示程序窗口
        p.StartInfo.CreateNoWindow = false;
        //启动程序
        p.Start();

        //向cmd窗口发送输入信息
        //p.StandardInput.WriteLine("ipconfig");
        p.StandardInput.WriteLine("mkdir vuepress-starter");
        //p.StandardOutput.ReadToEnd();
        p.StandardInput.WriteLine("cd vuepress-starter");
        //p.StandardOutput.ReadToEnd();

        p.StandardInput.WriteLine("npm install -D vuepress@next&&exit");
        //p.StandardOutput.ReadToEnd();
        

        p.StandardInput.AutoFlush = true;

        //获取输出信息
        string strOuput = p.StandardOutput.ReadToEnd();
        //等待程序执行完退出进程
        p.WaitForExit();
        p.Close();

        return strOuput;
    }
}



public class HelloWorldWorkflow : IWorkflow
{
    public string Id => "HelloWorld";
    public int Version => 1;

    public void Build(IWorkflowBuilder<object> builder)
    {
        builder
            .StartWith<HelloWorld>()
            .Then<GoodbyeWorld>()
            
            .EndWorkflow();
    }
}

public class HelloWorld : StepBody
{
    public override ExecutionResult Run(IStepExecutionContext context)
    {
        Console.WriteLine("你好");
        return ExecutionResult.Next();
    }

    
}

public class GoodbyeWorld : StepBody
{
    public override ExecutionResult Run(IStepExecutionContext context)
    {
        Console.WriteLine("再见");
        return ExecutionResult.Next();
    }
}

public class PassData
{
    public string Name { get; set; }

    public string Description { get; set; }
}

public class TestFlow : IWorkflow<PassData>
{
    public string Id => "TestFlow";

    public int Version => 1;

    public void Build(IWorkflowBuilder<PassData> builder)
    {
        //builder.StartWith<HelloWorld>()
        //       .If(data => data.Name == "sb")
        //       .Do(then => then.StartWith<>)
        //       .If(data => data.Name == "haha")
        //       .Do(then => then.StartWith<>)
        //       .Then<GoodbyeWorld>()
        //       .EndWorkflow();
    }
}

